import request from '@/utils/request.js'

// 获取图片验证码（登录用）
export const userLoginImageCheckCodeService = (formData) => {
  return request.get('/user/user/getLoginImageCheckCode', {
    responseType: 'blob'
  });
}

// 获取图片验证码（注册用）
export const userRegisterImageCheckCodeService = (formData) => {
  return request.get('/user/user/getRegisterImageCheckCode', {
    responseType: 'blob'
  });
}

// 获取 注册用的 邮箱验证码
export const userSendRegisterEmailCheckCodeService = (formData) => {
  return request.get('/user/user/sendRegisterEmailCheckCode', {
    params: formData,
  });
}

// 校验邮箱验证码之前发送的图形验证码（重置密码用）
export const userSendEmailCheckCodeForResetPasswordService = (formData, errorCallBack) => {
  return request.get('/user/user/sendEmailCheckCodeForResetPassword', {
    params: formData,
    showLoading: true,
    errorCallBack: errorCallBack
  });
}

// 注册
export const userRegisterService = (registerData) => {
  return request.post('/user/user/register', registerData);
}

// 登录（若登录失败，则调用回调函数，重新获取图片验证码）
export const userLoginService = (formData, errorCallBack) => {
  return request.post('/user/user/login', formData, {
    errorCallBack: errorCallBack
  })
}

// 提供调用用户重置密码接口的函数
export const userResetPasswordService = (formData) => {

  const params = new URLSearchParams()
  for (let key in formData) {
    params.append(key, formData[key])
  }

  return request.post('/user/user/resetPassword', params, { showLoading: true })
}

// 获取个人信息
export const userInfoGetService = (formData) => {
  return request.get('/user/user/getUserInfo', {
    params: formData,
  });
}

// 获取用户详细信息
export const userDetailService = (formData) => {
  return request.get('/user/user/getUserDetail', {
    params: formData,
  });
}

// 根据Token获取用户信息
export const getUserInfoByTokenService = (formData) => {
  return request.get('/user/user/getUserInfoByToken', {
    params: formData,
  });
}

// 获取用户发表的评论
export const userCommentService = (formData) => {
  return request.get('/user/user/getUserComment', {
    params: formData,
  });
}

// 修改个人信息
export const userInfoUpdateService = (userInfo) => {
  return request.post('/user/user/updateUserInfo', userInfo)
}

// 修改头像
export const userAvatarUpdateService = (formData) => {
  // let params = new URLSearchParams();
  // params.append('avatarUrl', avatarUrl)
  // return request.patch('/user/user/updateAvatar', params)

  return request.post('/user/user/uploadUserAvatar', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  })
}

// 获取用户发表的文章
export const userArticleService = (formData) => {
  return request.get('/user/user/getUserArticle',
    { params: formData }
  )
}